package com.shop.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.entity.logs;
import com.shop.service.adminserviceI;
import com.shop.service.adminserviceImpl;
import com.shop.service.logsserviceI;
import com.shop.service.logsserviceImpl;

/**
 * Servlet implementation class adminservlet
 */
@WebServlet("/adminservlet")
public class adminservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * webview 跳转
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String login = request.getParameter("login");
		if ("out".equals(login)) {
			// 销毁session
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("../1-2-3/test1/login.html");
		}
		
		/******删除用户记录，  在logs表里 *********/
		/*logsserviceI ls=new logsserviceImpl();
		try {
			ls.delete(Integer.valueOf(request.getParameter("id")));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		/***************************************/
		/*
		 * 
		 */
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		System.out.println(username + ":" + password);


		// 创建业务层个对象
		adminserviceI asi = new adminserviceImpl();
		try {
			if (asi.login(username, password)) {
				// 获取一个会话 将用户值存入会话中
				HttpSession session = request.getSession();
				session.setAttribute("username", username);
				
				
				/******添加用户登陆记录，  在logs表里 *********/
				logsserviceI lsi = new logsserviceImpl();
				logs logs = new logs();
				logs.setType("登录");
				logs.setNeirong(username + "登录成功");
				logs.setAid(username);
				lsi.add(logs);
				/**************************** *******/
				
				response.sendRedirect("../1-2-3/test1/index.jsp");
			} else {
				response.sendRedirect("../1-2-3/test1/errorpage.jsp");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
